<template>
	<view class="order-list">
		<view class="head">
			<liuyuno-tabs class="tabs" :tabData="tabs" :defaultIndex="0" @tabClick='tabClick' />
		</view>
		<view class="list-info">
			<view class="no-order" v-if="order_list.length == 0">
				<view class="no-list">
					<image src="../../static/img/mine/zanwu.png"></image>
				</view>
			</view>
			<block v-for="(item,index) in order_list" :key="index">
				<view class="items">
					<view class="items-up" @click="goDetail(item)">
						<view class="title-parent">
							<view class="title-left">
								<text class="iconfont icon-home"></text>
								<text>甘肃文创</text>
							</view>
							<view class="status">
								<text v-if="item.status === '1'">等待付款</text>
								<text v-if="item.status === '2'">等待发货</text>
								<text v-if="item.status === '3'">已发货</text>
								<text v-if="item.status === '4'&& item.afterSale!=4">已收货</text>
								<text v-if="item.status === '5'">交易关闭</text>
								<text v-if="item.status === '6'&& item.refundStatus==='1' ">等待退款中</text>
								<text v-if="item.status === '6'&& item.refundStatus==='2' ">退款中</text>
								<text v-if="item.status === '6'&& item.refundStatus==='3' ">退款成功</text>
								<text v-if="item.afterSale === '4'&&item.refundStatus==='1'">退款失败</text>
							</view>
						</view>
						<!-- <view class="items-info" v-if="item.goodsOrderDetailList.length>1">
							<block v-for="(item,index) in item.goodsOrderDetailList" :key="index">
							<view class="left">
								<image :src="item.listImage"></image>
							</view>
							</block>
							<view class="right">
								<view class="right-up">
									<text>{{item.goodsOrderDetailList[0].title}}</text>
								</view>
								<view class="right-down">
									<view>
										<text>共{{item.goodsOrderDetailList[0].sum}}件商品</text>
									</view>
									<view>
										<text>总计：</text>
										<text v-if="item.refundStatus">¥{{item.payAmount}}</text>
										<text v-else>¥{{item.payAmount}}</text>
									</view>
								</view>
							</view>
						</view> -->
						<view class="items-info">
							<view class="left">
								<image :src="item.goodsOrderDetailList[0].listImage"></image>
							</view>
							<view class="right">
								<view class="right-up">
									<text>{{item.goodsOrderDetailList[0].title}}</text>
								</view>
								<view class="right-down">
									<view>
										<text>共{{item.goodsOrderDetailList[0].sum}}件商品</text>
									</view>
									<view>
										<text>总计：</text>
										<text v-if="item.refundStatus">¥{{item.payAmount}}</text>
										<text v-else>¥{{item.payAmount}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="items-down">
						<view class="cancel" @click="cancelPay(item)" v-if="item.status === '1'">
							<text>取消订单</text>
						</view>
						<view class="pay" @click="goPay(item)" v-if="item.status === '1'">
							<text>付款</text>
						</view>
						<view class="del" @click="delOrder(item)" v-if="item.status === '5' ">
							<text>删除订单</text>
						</view>
						<view class="del pay" @click="goEnsure(item)" v-if="item.status === '3'">
							<text>确认收货</text>
							
						</view>
						<view class="del" @click="goDetail(item)" v-if="item.status == '2' || item.status == '5'">
							<text>订单详情</text>
						</view>
						<view class="del" @click="delOrder(item)" v-if=" item.refundStatus === '3'|| item.status === '4'&& item.afterSale!=4">
							<text>删除订单</text>
						</view>
						<view class="del pay" @click="checkdetail(item)" v-if=" item.refundStatus === '1' || item.refundStatus === '2'||item.refundStatus === '3' || item.afterSale==='4'">
							<text>查看详情</text>
						</view>
						<view class="del pay" @click="refund(item)" v-if="item.status === '4'">
							<text>申请退款</text>
						</view>
					</view>
				</view>
				
			</block>
		</view>
		<view class="foot" :class="order_list.length > 2 ? '' : 'foot-fixed'">
			<footLogo></footLogo>
		</view>
		<ensure
			:isMutipleLine="false" 
			:visible.sync="promptVisible" 
			inputStyle="" 
			:title="title"
			mainColor="rgba(51,51,51,1)" 
			@confirm="clickPromptConfirm" 
			ensure="确定" 
			ensureColor="rgba(0,0,0,1)"
			titleColor="rgba(51,51,51,1)" 
			titleSize="36"
			fontWeight="400"
		  >
		</ensure>
	</view>
</template>

<script>
	import liuyunoTabs from "@/components/liuyuno-tabs/liuyuno-tabs.vue";
	import footLogo from "@/components/foot-logo/footLogo.vue";
	import ensure from "@/components/confirm/confirm.vue"
	export default {
		components: {
			liuyunoTabs,
			footLogo,
			ensure
		},
		data() {
			return {
				tabs: ['全部', '待付款', '待发货', '待收货', '已完成'],
				order_list: [],
				ordernum:'',
				promptVisible:false,
				orderNumber:'',
				id:'',
				title:'',
				choose:'',
				tabChoose:'',
				totalAmount:''
			};
		},
		onLoad: function(option) {
			this.getList()
		},
		methods: {
			//退款详情页
			checkdetail:function(item){
					uni.navigateTo({
						url:'/pages/order/neworderdetail?bustype=3&id=' + item.id + '&orderNumber=' + item.orderNumber + '&type=3'
					})
			},
			//获取售后详情
			getAftersaleInfo:function(order){
				console.log("order-------------->",order)
				var params = {
					orderNumber:order.orderNumber
				}
				this.$postApi(this.$path.GOODS_AFTERSALE_LIST,params).then(res=>{
					console.log("返回的售后详情-------------->",res)
					if(res.data.success){
						for(var i=0;i<1;i++){
							//判断退款状态
							order.refundStatus= res.data.data.list[0].refundStatus
							//afterSale------>售后状态status
							order.afterSale= res.data.data.list[0].status
							//总计
							order.totalPrice=  (parseFloat(res.data.data.list[0].unitPrice)*parseFloat(res.data.data.list[0].goodsSum)).toFixed(2)
							//status为6的数据追加到status为4数据的后面
							this.order_list.push(order)
						}
						console.log("this.order_list------------>",this.order_list)
					}
				})
			},
			//申请退款
			refund: function(item) {
				uni.redirectTo({
					url: '/pages/order/aftersale?item=' + encodeURIComponent(JSON.stringify(item)),
					animationType: 'pop-in',
					animationDuration: 200
				});
				
			},
			//确认收货
			goEnsure:function(item){
				//显示弹窗
				this.promptVisible = true
				this.title = "是否确认收货"
				this.orderNumber = item.orderNumber
				this.id = item.id
				this.choose = '1'
			},
			//删除订单
			delOrder: function(item) {
				//显示弹窗
				this.promptVisible = true
				this.title = "是否删除此订单"
				this.id = item.id
				this.choose = '2'
			},
			//取消订单
			cancelPay: function(item) {
				//显示弹窗
				this.promptVisible = true
				this.title = "确认取消该订单吗?"
				this.id = item.id
				this.orderNumber = item.orderNumber
				this.choose = '3'
			},
			//弹窗成功
			clickPromptConfirm: function() {
				let params = {}
				switch(this.choose){
					//确认收货
					case "1":
					 params = {
						id: this.id,
						orderNumber: this.orderNumber,
						status: '4'
					}
					this.$postApi(this.$path.GO_GOODS,params).then(res => {
						console.log("确认收货返回结果------------>",res.data)
						if(res.data.success){
							this.promptVisible = false
							this.pub.showError('收获成功')
							//tabChoose==3==待收货
							if(this.tabChoose==3){
								this.getList(3)
								return
							}
							//tabChoose==0==全部
							this.getList()
						}else{
							this.pub.showError(res.data.error.errorMessage)
						}
					})
						break;
						//删除
					case "2":
						params = {
							id: this.id,
						}
						this.$postApi(this.$path.GOODS_ORDER_DELETE,params).then(res => {
							console.log("删除订单返回结果------------>",res.data)
							if(res.data.success){
								this.promptVisible = false
								this.pub.showError('删除成功')
								//tabChoose==4==已完成
								if(this.tabChoose==4){
									this.getList(4)
									return
								}
								//tabChoose==0==全部
								this.getList()
							}else{
								this.pub.showError(res.data.error.errorMessage)
							}
						})
						break;
						//取消订单
					case "3":
						params = {
							id: this.id,
							orderNumber: this.orderNumber,
							status: '5'
						}
						this.$postApi(this.$path.GOODS_ORDER_STATUS, params).then(res => {
							console.log("取消订单返回结果------------>", res.data)
							if (res.data.success) {
								this.promptVisible = false
								this.pub.showError("订单取消成功")
								//tabChoose==4==待付款
								if(this.tabChoose==1){
									this.getList(1)
									return
								}
								//tabChoose==0==全部
								this.getList()
							} else {
								this.pub.showError(res.data.error.errorMessage)
							}
						})
						break;
				}
			},
			//订单详情
			goDetail: function(item) {
				if(item.refundStatus === '1' || item.refundStatus === '2'||item.refundStatus === '3' || item.afterSale==='4'){
					//退款详情页
					this.checkdetail(item)
					return
				}
				uni.navigateTo({
					url: '/pages/orderlist/orderdetail?id=' + item.id + '&orderNumber='+item.orderNumber,
					animationType: 'pop-in',
					animationDuration: 200
				});
			},
			//去支付
			goPay: function(item) {
				let data = {
					payAmount: item.goodsOrderDetailList[0].totalAmount,
					orderId: item.id
				};
				uni.navigateTo({
					url: '/pages/scenicOrder/counter?data=' + JSON.stringify(data) + '&bustype=3',
					animationType: 'pop-in',
					animationDuration: 200
				});
			},
			tabClick: function(e) {
				console.log("tabClick----------------------->",e)
				this.tabChoose = e
				this.getList(e)
			},
			//获取订单列表
			getList: function(status) {
				let params = {
						memberId: this.pub.getUserInfo().memberId,
						// memberId: this.pub.getUserInfo().memberId || "4f0abd1af84d4168ba27ac00b63fcb2e",
						pageIndex: '1',
						pageSize: '10',
						//订单状态(1待付款2待发货3待收货4已完成5已取消6售后中)
						status: status || ''
					}
				this.$postApi(this.$path.GOODS_ORDER_LIST, params).then(res => {
					console.log("订单列表返回--------------->", res.data)
					if (res.data.success) {
						if(status==4){
							this.order_list = res.data.data.list
							this.getList(6);
						}else if(status==6){
							res.data.data.list.forEach(res=>{
								// 获取售后详情
								this.getAftersaleInfo(res)
							})
						}else{
							this.order_list = res.data.data.list
							this.order_list.forEach(res=>{
								this.getAftersaleInfo(res)
							})
							console.log("this.order_list---------->",this.order_list)
						}
					} else {
						this.order_list = []
						//status4没有数据在看status6有没有数据
						if(status==4){
							this.getList(6)
						}
						this.pub.showError(res.data.error.errorMessage)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.order-list {
		background: #f2f2f2;
		padding: 0;
		margin: 0;
		font-size: 34upx;

		image {
			width: 100%;
			height: 100%;
		}

		min-height: 100%;
	}

	.head {
		// height: 100upx;
		padding-left: 40upx;
		background: transparent;

		.tabs {
			font-size: 28upx;
			box-shadow: 0 0px 0px rgba(0, 0, 0, 0.0);
			background: #f2f2f2;

			// padding-left: 30upx;
			.underline {
				padding: 0 !important;
			}
		}
	}

	.list-info {


		.items {
			background: #fff;
			padding: 30upx;
			margin-top: 12upx;
			height: 358upx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		}

		.title-parent {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.title-left {
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: rgba(26, 26, 26, 1);
			}

			.status {
				font-size:24upx;
				font-family:PingFang SC;
				font-weight:400;
				color:rgba(102,102,102,1);
			}
		}

		.items-info {
			display: flex;
			margin-top: 30upx;
			border-bottom: 1upx solid #F8F8F8;
			padding-bottom: 40upx;

			.left {
				width: 112upx;
				height: 112upx;
				background: rgba(149, 149, 198, 1);
				border-radius: 7upx;

				image {
					border-radius: 7upx;
				}
			}

			.right {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				width: 80%;
				margin-left: 20upx;

				.right-up {
					font-size: 34upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: rgba(51, 51, 51, 1);
					overflow: hidden;
					text-overflow:ellipsis;
					white-space: nowrap;
				}

				.right-down {
					display: flex;
					justify-content: space-between;

					view:first-child {
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: rgba(102, 102, 102, 1);
					}

					view:last-child {
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: rgba(102, 102, 102, 1);

						text:last-child {
							color: rgba(255, 94, 4, 1);
							font-size: 30upx;
						}
					}
				}
			}
		}

		.items-down {
			display: flex;
			justify-content: flex-end;
			align-items: center;

			view {
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 26upx;
				width: 139upx;
				height: 55upx;
				border: 1px solid rgba(204, 204, 204, 1);
				border-radius: 28upx;
				margin-left: 14upx;
			}

			.cancel,
			.del {
				color: rgba(128, 128, 128, 1);
			}

			.pay {
				color: rgba(255, 94, 4, 1);
				border: 1px solid rgba(255, 94, 4, 1);
			}
		}
	}
	.no-order{
		padding: 20upx;
		margin-top: 150upx;
		// height: 300upx;
	}
	.no-list{
		height: 435upx;
	}
	.foot{
		width: 100%;
	}
	.foot-fixed{
		position: fixed;
		bottom: 0;
	}
</style>
